/*
 * 首页左侧菜单
 * hanwei
 */
(function(factory) {
	if (typeof define === "function" && define.amd) {
		define([ "jquery" ], factory);
	} else {
		factory(jQuery);
	}
}
		(function($) {
			var menuBody = [];
			var menu_opt = null;
			var leftMenu_height = $(window).height() - 50;
			var clickId = "fun";
			var LeftMenu = function(opt, clickCallback) {
				// 添加clickCallback参数,改参数可选,点击菜单显示/隐藏按钮之后 运行的回调函数
				function init(_opt) {
					if (!_opt)
						_opt = [];
					setMenuBody();
					var resbar=new resizeObject("#menuDragBar");//启用resizebar
					$(menuBody.join('')).appendTo($(".menuBody"));
					setMenuLevelTwo(_opt);
					setStyle();
					setEvents();				
					$(".cycd").mouseenter(
									function() {
										$(".mtlb").html("");
										$(".menu").removeClass("mouseIn");
										$(".na").hide();
										$(this).addClass("cycd_border");
										var menuC = getCommonMenu();
										$("#menuThreeLevel").css("display", "block");
										$("#clickMenuImg").attr("src", $(this).find("img").attr("src"));
										$(".clickMenuName").text($(this).find("div[class='cycd-text']").text());
										$(".clickMenuName").attr("title", $(this).find("div[class='cycd-text']").text());
										if ($(this).hasClass("cycdcolor")) {
											// $(this).removeClass("cycdcolor");
										} else {
											$(this).addClass("cycdcolor");
										}
										$(this).find("div[class='cycd-img']").addClass("cycd-img-hover");
										// 样式重置
										leftMenu_height = $(window).height() - 50;
										setStyle();
									})
							.mouseleave(function() {
										if ($("#menuThreeLevel").css("display") != 'block') {
											$(this).removeClass("cycd_border");
											$(this).find("div[class='cycd-img cycd-img-hover']")
												   .removeClass("cycd-img-hover");
										}
									});
					$("#topMenuStart").mouseenter(function() {
								$("#menuThreeLevel").hide();
							});
				}

				/*
				 * 构造菜单 显示构架
				 */
				function setMenuBody() {
					menuBody.push("<div class='leftMenu' >"); // 菜单最外层div
					menuBody.push("<div class='controlMenu show' data-state='show'></div>");// 控制菜单显示和隐藏的按钮add by chenyao					
					/**
					 * *************************常用菜单部分* start*************************/
					menuBody.push("<div class='cycd' >"); // 常用菜单
					menuBody.push("<div class='cycd-img'  >"); // 常用菜单图片
					menuBody.push("</div>");
					menuBody.push("<div class='cycd-text'>常用菜单</div>"); // 常用菜单文字
					menuBody.push("</div>");
					/*** *************************常用菜单部分* end*************************/
					/*** *************************具体二级菜单部分* start*********************/
					menuBody.push("<div class='twoMenu'  >");
					menuBody.push("</div>");
					/*** *************************具体二级菜单部分* end***********************/
					menuBody.push("</div>");
					/*** *************************具体三级菜单部分* start***********************/
					menuBody.push("<div id='menuThreeLevel'  class='menu_tl' >");
					menuBody.push("<div class='menuThreeLevel_top'  >");
					menuBody.push("<div class='menuThreeLevel_top1'  >");
					menuBody.push("<div  class='clickMenuName'>常用菜单</div>");
					menuBody.push("<div class='closeMenu' >");
					menuBody.push("</div>");
					menuBody.push("</div>");
					menuBody.push("</div>");
					menuBody.push("<hr class='hr_warm' />");
					menuBody.push("<div class='mtlb' style='  overflow-y: auto;'>");
					menuBody.push("</div>");
					/*** *************************具体三级菜单部分 * end***********************/
					menuBody.push("<div class='na' > " +
					"</div>");
				}
				function setStyle() {
					$(".leftMenu").css({
						"height" : leftMenu_height
					});
					$(".twoMenu").css(
							{
								"height" : $(".leftMenu").height()
										- $(".cycd").height()
							});
					$(".menu_tl").css("height", $(".leftMenu").height() + 10);
					$(".mtlb").css(
							"height",
							$(".menu_tl").height()
									- $(".menuThreeLevel_top").height() - 60);
				}
				function setEvents() {
					var menuTwoLevel_delay;
					$(".menu").mouseenter(
									function() {
										$(this).addClass("mouseIn");
										$(this).find("div[class='menu_img']")
												.addClass("menu_img_hover");
										$(".cycd").removeClass("cycd_border");
										$(
												"div[class='cycd-img cycd-img-hover']")
												.removeClass("cycd-img-hover");
										$(".cycd").removeClass("cycdcolor");
									})
							.mouseleave(
									function() {
										$(".menu").removeClass("mouseIn");
										$(this)
												.find(
														"div[class='menu_img menu_img_hover']")
												.removeClass("menu_img_hover");
										if (menuTwoLevel_delay) {
											clearTimeout(menuTwoLevel_delay);
										}
									})
							.mouseover(
									function() {
										$(".menu").removeClass("mouseIn");
										var $menu = $(this);
										clickId = $menu.attr("id");
										$menu.addClass("mouseIn");
										menuTwoLevel_delay = setTimeout(
												function() {
													var $menuUrl = $menu.find(".menuleft_text").attr("data-url");
													if ($menuUrl != "null") {
														//IndexTabCsi.addTab(clickId,$menu.find("div[class='menuleft_text']").text(),$menuUrl,function(){});
														$("#menuThreeLevel")
																.hide();
													}
													var flag = setMenuLevelThree($menu
															.attr("id"));
													if (!flag) {
														return;
													}
													var top = $menu.offset().top;
													var left = $menu.offset().left;
													top = top
															+ ($menu.height())
															/ 2
															- $menu.height()
															- 7;
													left = left + $menu.width();
													$("#clickMenuImg")
															.attr(
																	"src",
																	$menu
																			.find(
																					"img")
																			.attr(
																					"src"));
													$(".clickMenuName")
															.text(
																	$menu
																			.find(
																					"div[class='menuleft_text']")
																			.text());
													$(".clickMenuName")
															.attr(
																	"title",
																	$menu
																			.find(
																					"div[class='menuleft_text']")
																			.text());
													$(".na").css({
														"left" : "-8px",
														"top" : top,
														"display" : "block"
													});
													if ($(".menu_tl").css(
															"display") == "none") {
														$(".menu_tl").css(
																"display",
																"block");
													}
												}, 500);

									})
							.click(
									function(e) {
										$(".menu").removeClass("mouseIn");
										var $menu = $(this);
										clickId = $menu.attr("id");
										$menu.addClass("mouseIn");
										var $menuUrl = $menu.find(".menuleft_text").attr("data-url");
										if ($menuUrl != "null" && $menuUrl && $menuUrl != null && $menuUrl!="undefined") {
											IndexTabCsi
													.addTab(
															clickId,
															$menu
																	.find(
																			"div[class='menuleft_text']")
																	.text(),
															$menuUrl,
															function() {
											});
										}
										var flag = setMenuLevelThree($menu
												.attr("id"));
										if (!flag) {
											return;
										}
										var top = $menu.offset().top;
										var left = $menu.offset().left;
										top = top + ($menu.height()) / 2
												- $menu.height() - 7;
										left = left + $menu.width();
										$("#clickMenuImg").attr("src",
												$menu.find("img").attr("src"));
										$(".clickMenuName")
												.text(
														$menu
																.find(
																		"div[class='menuleft_text']")
																.text());
										$(".clickMenuName")
												.attr(
														"title",
														$menu
																.find(
																		"div[class='menuleft_text']")
																.text());
										$(".na").css({
											"left" : "-8px",
											"top" : top,
											"display" : "block"
										});
										if ($(".menu_tl").css("display") == "none") {
											$(".menu_tl").css("display",
													"block");
										}
									});

					// 三级菜单
					$(".menu_tl").mouseleave(
							function() {
								$(this).css("display", "none");
								$(".na").css("display", "none");
								$(".menu").removeClass("mouseIn");
								$(".cycd").removeClass("cycdcolor");
								$(".cycd").removeClass("cycd_border");
								$("div[class='cycd-img cycd-img-hover']")
										.removeClass("cycd-img-hover");
							});
					// 关闭图片点击事件
					$(".closeMenu").click(
							function() {
								$(".menu_tl").css("display", "none");
								$(".na").css("display", "none");
								$(".menu").removeClass("mouseIn");
								$(".cycd").removeClass("cycdcolor");
								$(".cycd").removeClass("cycd_border");
								$("div[class='cycd-img cycd-img-hover']")
										.removeClass("cycd-img-hover");
							});
					// 控制菜单显示和隐藏 add by chenyao					
					$(".controlMenu").unbind("click").click(function() {
						if ($(this).attr("data-state") == "show") {
							$(this).attr("data-state", "hide");
							$(".controlMenu").animate({
								
							}).addClass("hide").removeClass("show");
							var wd = $(".leftMenu").width();
							var timer;
					
							$("#menuDragBar").css({"left":"27px"})
							$(".menuBody").animate({
								"width" : 27 + "px"
							});	
							$("#mainFrameBox").css({"display":"none"});
				    		$("#bc_tabs").css({"display":"none"}); 	
							$("#csi_index_tab_iframe").animate({"padding-left":"27px"},function(){
							
								$("#mainFrameBox").css({"display":"block"});
					    		$("#bc_tabs").css({"display":"block"}); 
					    		fnInitPage();
							});
						
		
							return;
						} else {
							$(this).attr("data-state", "show");
							$(".controlMenu").animate({
							   
							}).addClass("show").removeClass("hide");
					
							$("#menuDragBar").css({"left":"173px"})
							$(".menuBody").animate({
								"width" : 180 + "px"
							});
							$("#mainFrameBox").css({"display":"none"});
				    		$("#bc_tabs").css({"display":"none"});
							$("#csi_index_tab_iframe").animate({"padding-left":"180px"},function(){
							
								$("#mainFrameBox").css({"display":"blcok"});
					    		$("#bc_tabs").css({"display":"block"});
					    		fnInitPage();
							});
							
							return;
						}
					})
					

			   

				}

				function setMenuLevelTwo(obj) {
					$(".mtlb").html("");
					menu_opt = obj;
					if (!obj)
						obj = [];
					var menuLevelTwo = [];
					for (var i = 0; i < obj.length; i++) {
						menuLevelTwo.push("<div id='" + obj[i].menuId + "' class='menu'>");
						menuLevelTwo.push("<div title='" + obj[i].menuName + "' class='menuleft_text' data-url='"+obj[i].url+"'> "+obj[i].menuName +"</div>");
						menuLevelTwo.push("</div>");
					}
					$(".twoMenu").html($(menuLevelTwo.join('')));
					setEvents();
				}
				function setMenuLevelThree(menuid) {
					var menuThreeLevel_delay;
					var menu_three_opt = null;
					var menu_three = [];
					for (var i = 0; i < menu_opt.length; i++) {
						if (menu_opt[i].menuId == menuid) {
							menu_three_opt = menu_opt[i].childNode;
							break;
						}
					}
					if (!menu_three_opt || menu_three_opt.length==0 ) {
						return false;
					} else {
						for (var i = 0; i < menu_three_opt.length; i++) {
							menu_three.push("<div class='positionClass'>");
							menu_three.push("<div class='threeMenuImg'>");
							// menu_three.push( "<img
							// src='indexue/images/threemenuimg.png' style='
							// margin-top: 12px;' />");
							menu_three.push("</div>");
							menu_three
									.push("<div style='float:left;margin-left:6px;height:100%;'>");
							menu_three.push("<span title='"
									+ menu_three_opt[i].menuName
									+ "' class='menuNames'>"
									+ menu_three_opt[i].menuName + "</span>");
							menu_three
									.push("<span id='"
											+ menu_three_opt[i].menuId
											+ "' style='display:none;' class='csimenu_url'>"
											+ menu_three_opt[i].url + "</span>");
							menu_three.push("</div>");
							if (menu_three_opt[i].childNode) {
								menu_three
										.push("<div class='haveNextMenu' ></div>");
							}
							menu_three.push("</div>");
						}
						$(".mtlb").html(menu_three.join(''));
					}

					$(".positionClass")
							.mouseenter(
									function() {
										$(this).addClass("divoverClass");
										$(this).find(
												"div[class='threeMenuImg']")
												.addClass("threeMenuImg1");
										$(".cycd").removeClass("cycd_border");
										$(
												"div[class='cycd-img cycd-img-hover']")
												.removeClass("cycd-img-hover");
										$(".cycd").removeClass("cycdcolor");
										var $nowMenu = $(this);
										menuThreeLevel_delay = setTimeout(
												function() {
													$(".nextMenu").remove();
													setMenuLevelFour_next(
															menu_three_opt[$nowMenu
																	.index()],
															$nowMenu, 4);
												}, 300);
									})
							.mouseleave(
									function() {
										$(this).removeClass("divoverClass");
										$(this)
												.find(
														"div[class='threeMenuImg threeMenuImg1']")
												.removeClass("threeMenuImg1");
										if (menuThreeLevel_delay) {
											clearTimeout(menuThreeLevel_delay);
										}
									})
							.click(
									function() {
										var $span1 = $(this).find("span").eq(0);
										var $span2 = $(this).find(
												"span[class='csimenu_url']");
										if ($span2.text() != "null" && $span2.text()!=null && $span2.text() && $span2.text()!="undefined") {
											IndexTabCsi.addTab($span2
													.attr("id"), $span1.text(),
													$span2.text(), function() {
													});
											$("#menuThreeLevel").hide();
										}
									});
					return true;
				}

				//yanglq 4级+ 菜单
				function setMenuLevelFour_next(nextMenu, $menu, level) {
					var menu_next;
					if (!nextMenu.childNode) {
						return false;
					} else {
						var menu_childNode = nextMenu.childNode;
						var left = $menu.width();
						if (level >= 5) {
//							left = $menu.offset().left ;
							left = parseInt($menu.parent().css("left"))+$menu.width();
						}
                        var top = $menu.offset().top - 40;
                        var mintop =  $(".mtlb").find(".positionClass").eq(0).offset().top - 40;
                        var onemenuheight = 35;
                        var menudivheight = menu_childNode.length * 35;
                        var menudivbottom = $(window).height() - ($menu.offset().top - $(document).scrollTop()) - $menu.outerHeight();
                        //底部高度不够时
                        if(menudivbottom < menudivheight){
                            top = top - (menudivheight -menudivbottom) + 25;
                            if(top < mintop) top = mintop;
                        }
						menu_next = "<div id='nextMenu_" + level
								+ "' class='nextMenu' style='top:" + "30px;"
								+ "px;left:" + left + "px;'>";
						for (var i = 0; i < menu_childNode.length; i++) {
							menu_next += "<div class='four_next_"
									+ level
									+ "' style='height:" + onemenuheight + "px;'><div class='nextMenuLevel'><div class='nextMenuImg'></div>"
									+ "<span title='"
									+ menu_childNode[i].menuName
									+ "' class='menuNames'>"
									+ menu_childNode[i].menuName
									+ "</span>"
									+ "<span id='"
									+ menu_childNode[i].menuId
									+ "' style='display:none;' class='csimenu_url'>"
									+ menu_childNode[i].url + "</span>"
									+ "</div>";
							if (menu_childNode[i].childNode) {
								menu_next += "<div class='haveNextMenu' ></div>";
							}
							menu_next += "</div>"
						}
						menu_next += "</div>";
						$(".mtlb").append(menu_next);
						var $nowMenu = $("#nextMenu_" + level);
						var height = document.documentElement.clientHeight
								- $nowMenu.offset().top;
						if (height < $nowMenu.height()) {
							$nowMenu.css({
								"max-height" : height,
								"overflow-y" : "auto"
							});
						}
						$(".four_next_" + level).mouseenter(
								function() {
									$(this).addClass("divoverClass");
									$("#nextMenu_" + (level + 1)).remove();
									$("#nextMenu_" + (level + 2)).remove();
									setMenuLevelFour_next(
											menu_childNode[$(this).index()],
											$(this), level + 1);
								}).mouseleave(function() {
							$(this).removeClass("divoverClass");
						}).click(
								function() {
									var $span1 = $(this).find("span").eq(0);
									var $span2 = $(this).find(
											"span[class='csimenu_url']");
									if ($span2.text() != "null" && $span2.text()!=null && $span2.text() !="undefined") {
										IndexTabCsi.addTab($span2.attr("id"),
												$span1.text(), $span2.text(),
												function() {
												});
										$("#menuThreeLevel").hide();
									}
								});
					}
				}

				$.extend(this, {
					"init" : init,
					"setMenuLevelTwo" : setMenuLevelTwo,
					"hideLeftMenu":hideLeftMenu


				});

			}
			function hideLeftMenu(){
				if ($("#menuThreeLevel").css("display") != 'block') {
					$(".cycd").removeClass("cycd_border");
					$(".cycd")
						.find(
							"div[class='cycd-img cycd-img-hover']")
						.removeClass(
							"cycd-img-hover");
				}

				$(".menu_tl").css("display", "none");
				$(".na").css("display", "none");
				$(".menu").removeClass("mouseIn");
				$(".cycd").removeClass("cycdcolor");
				$(".cycd").removeClass("cycd_border");
				$("div[class='cycd-img cycd-img-hover']")
					.removeClass("cycd-img-hover");
			}
			function reSize(height) {
				$(".leftMenu").css({
					"height" : height
				});
				$(".twoMenu").css({
					"height" : $(".leftMenu").height() - $(".cycd").height()
				});
				$(".menu_tl").css({
					"height" : height
				});

			}

			// 拖动 条控制菜单 add by chenyao
		    function resizeObject(el) {	 
				var self=this;
				self.mouse_top =0;
				self.mouse_left=0;
				self.el=$(el);//拖动对象 
				self.lf= parseInt(self.el.css("left"));//拖动对象的left
				self.tp= parseInt(self.el.css("top"));//拖动对象的top		   
				self.el.bind("mousedown",function(e) {
		    	    self.mouse_left = e.clientX,
		    	    self.mouse_top = e.clientY;         
		    	 /*   $("#csi_index_tab_iframe").css({"display":"none"});  */ 
		    		$("#mainFrameBox").css({"display":"none"});
		    		$("#bc_tabs").css({"display":"none"}); 		    		
		            $(document).bind("mousemove", mouseMoveDrag).bind("mouseup", mouseUpDrag);
		            })	
		    	 //拖动改变位置
		         function mouseMoveDrag(e) {
		    		    var target = self.el;
		    	        var dx = e.clientX - self.mouse_left;
		    	        var dy = e.clientY - self.mouse_top;
		    	        var l = parseInt(target.css("left"));
		    	        var t = parseInt(target.css("top"));
		    	        var movex = l + dx;
		    	        var movey = t + dy;
		    	        if(movex<27){
		    	        	movex=27; 
		    	        }
		    	        if(movex>293){
		    	        	movex=293;
		    	        }
		    	        if(movex>50){
		    	        	$(".controlMenu").addClass("show").removeClass("hide").attr("data-state","show");	
		    	        }
		    	        if(movex<50){
		    	        	$(".controlMenu").addClass("hide").removeClass("show").attr("data-state","hide");	
		    	        }
		    	        self.mouse_left = e.clientX;
		    	        self.mouse_top = e.clientY;
		    	        self.lf=movex;
		    			self.tp=movey;
		    	        /* target.css({'left': movex+"px", 'top': movey+"px"}); */
		    	        target.css({'left': movex+"px"}); 
		    	        $(".menuBody").css("width",movex);
		    	        $("#csi_index_tab_iframe").css({"padding-left":movex})
	
		          };
		    	  function mouseUpDrag(e) { 		    		   
//		    		      $("#csi_index_tab_iframe").css({"display":"block"}); 
		    		  	$("#mainFrameBox").css({"display":"block"}); 
		    		  	$("#bc_tabs").css({"display":"block"}); 
		    		    fnInitPage();
		    		    $(document).unbind("mousemove", mouseMoveDrag).unbind("mouseup", mouseUpDrag);    		    
		          }			    			
		    	
		     }
		  
			
			$(window).resize(
					function() {
						reSize($(window).height() - 50);
						setTimeout(function() {
							$("#mainFrameBox >iframe").height(
									$("#mainFrameBox").height());
						}, 100);
						IndexTabCsi.adjustTabWidth();
					});
			$.extend(window, {
				"LeftMenu" : LeftMenu,
				"hideLeftMenu":hideLeftMenu

			});
			return LeftMenu;
		}));
